package com.beauty.order.repository;

import com.beauty.order.entity.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
    
    Optional<Order> findByOrderNumber(String orderNumber);
    
    List<Order> findByCustomerId(String customerId);
    
    List<Order> findByStatus(Order.OrderStatus status);
    
    List<Order> findByOrderDateBetween(LocalDateTime start, LocalDateTime end);
    
    List<Order> findByCustomerIdAndStatus(String customerId, Order.OrderStatus status);
    
    Page<Order> findAll(Pageable pageable);
    
    Page<Order> findByCustomerId(String customerId, Pageable pageable);
    
    Page<Order> findByStatus(Order.OrderStatus status, Pageable pageable);
    
    Page<Order> findByOrderDateBetween(LocalDateTime start, LocalDateTime end, Pageable pageable);
    
    Page<Order> findByOrderNumberContaining(String keyword, Pageable pageable);
    Page<Order> findByCustomerIdContaining(String keyword, Pageable pageable);
    Page<Order> findByTrackingNumberContaining(String keyword, Pageable pageable);
    Page<Order> findByNotesContaining(String keyword, Pageable pageable);
    Page<Order> findByShippingAddressContaining(String keyword, Pageable pageable);
    
    @Query("SELECT o FROM Order o WHERE " +
           "LOWER(o.orderNumber) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(o.customerId) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(o.trackingNumber) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(o.notes) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(o.shippingAddress) LIKE LOWER(CONCAT('%', ?1, '%'))")
    Page<Order> searchByKeyword(String keyword, Pageable pageable);
} 